# from pinyin import lazy_pinyin
filename = input()
filename2 = input()
province = input().split()
infile = open(filename, 'r')
outfile = open(filename2, 'w')
line = infile.readlines()
judge_different = ''
province_amount = 0
all_sort = []
dic = dict()

for i in province:
        for l in line:
            curline = l.strip().split("\t")
            if i != '':
                if curline[0] == i:
                    all_sort.append(curline)
                    province_amount = province_amount + int(curline[2])
            else:
                if province_amount != curline[0]:
                    a = curline[0]
                    outfile.write('\n')
                    outfile.write(curline[0])
                    outfile.write('\n')
                for out in range(1, 3):
                    outfile.write(curline[i])
                    outfile.write('\t')
                outfile.write('\n')
        dic[i] = province_amount
        dic1 = sorted(dic.items(), key=lambda x: x[1], reverse=True)

n = len(all_sort)
for j in range(0, n - 1):
    for i in range(0, n - 1 - j):
        if int(all_sort[i][2]) < int(all_sort[i + 1][2]):
            all_sort[i], all_sort[i + 1] = all_sort[i + 1], all_sort[i]

# for i in range(n):
#     c = []
#     for j in range(1, n-i):
#         if b[i][0] == b[i+j][0]:
#             c.append(b[i][0])
#             c.append(b[i+j][0])
#     c.sort(key=lambda char: lazy_pinyin(char)[0][0])
#     if c != '':
#         for z in range(0, j):
#             for r in b:
#                 if r[0] == c[z]:
#                     b[i][0] = r[0]

for i in dic1:
    outfile.write(i[0])
    outfile.write('\t')
    outfile.write(str(i[1]))
    outfile.write('\n')
    for j in all_sort:
        if j[0] == i[0]:
            outfile.write(j[1])
            outfile.write('\t')
            outfile.write(j[2])
            outfile.write('\n')
    outfile.write('\n')

outfile.close()
outfile.close()

